package io.gitee.chearnee.fw.rabbitmq.config;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

public class RabbitConfig {
    @Bean
    @ConditionalOnClass(value = {RabbitProperties.class, Jackson2ObjectMapperBuilder.class})
    public RabbitTemplateConfigurer rabbitTemplateConfigurer(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder, RabbitProperties properties,
                                                             ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers) {
        RabbitTemplateConfigurer configurer = new RabbitTemplateConfigurer(properties);
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(jackson2ObjectMapperBuilder.build());
        configurer.setMessageConverter(jackson2JsonMessageConverter);
        configurer
                .setRetryTemplateCustomizers(retryTemplateCustomizers.orderedStream().toList());
        return configurer;
    }
}
